package com.pur.zsms.matermanage.mater.web;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.pur.zsms.accountmanage.vo.AccountVo;
import com.pur.zsms.common.exception.GenericException;
import com.pur.zsms.common.filter.AttributeListenerUtil;
import com.pur.zsms.common.support.PageController;
import com.pur.zsms.common.support.PaginationSupport;
import com.pur.zsms.common.util.ApplicationUtil;
import com.pur.zsms.foodmanage.food.po.FoodPo;
import com.pur.zsms.foodmanage.food.service.IFoodService;
import com.pur.zsms.foodmanage.food.vo.FoodVo;
import com.pur.zsms.matermanage.mater.po.MaterPo;
import com.pur.zsms.matermanage.mater.service.IMaterService;
import com.pur.zsms.matermanage.mater.vo.MaterValueVo;
import com.pur.zsms.matermanage.mater.vo.MaterVo;
import com.pur.zsms.matermanage.mater.vo.ShoppingCartMaterVo;
import com.pur.zsms.pmsnmanage.role.vo.RoleVo;
import com.pur.zsms.pmsnmanage.user.po.UserPo;
import com.pur.zsms.po.CashierOrderDetailPo;
import com.pur.zsms.po.CashierOrderPo;
import com.pur.zsms.service.ICashierService;
import com.pur.zsms.service.impl.CashierServiceImpl;
import com.pur.zsms.shopmanage.service.IShopService;
import com.pur.zsms.supplier.service.ISupplierService;
import com.pur.zsms.systemsetting.datadictionary.po.Code;
import com.pur.zsms.systemsetting.datadictionary.service.IDataDictionaryService;
import com.pur.zsms.vo.CashierOrderDetailVo;
import com.pur.zsms.vo.CashierOrderVo;
import com.pur.zsms.vo.ShoppingCartCashierVo;

@Controller
@RequestMapping(value = "/cashier")
public class CashierControllor {

	@SuppressWarnings("unused")
	private static final Logger log = LoggerFactory.getLogger(CashierControllor.class);

	protected int page = 1;

	protected int pageSize = 15;

	PaginationSupport pageList;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	@Resource(name = "materService")
	private IMaterService materService;

	@Resource(name = "shopService")
	private IShopService shopService;
	@Resource(name = "foodService")
	private IFoodService foodService;
	@Resource(name = "supplierService")
	private ISupplierService supplierService;

	@Resource(name = "cashierService")
	private ICashierService cashierService;

	@RequestMapping(value = "/getMaterByBarcode.do", method = { RequestMethod.POST })
	@ResponseBody
	public String getMaterByBarcode(String barcode, HttpSession session) {
		JSONObject json = new JSONObject();
		try {

			MaterPo materpo = materService.getMaterPo(barcode);
			if (materpo != null) {
				json.put("success", true);
				json.put("materId", materpo.getMaterId());
				json.put("unit", materpo.getUnit());

				json.put("materName", materpo.getMaterName());
				json.put("materValue", 10);
			} else {
				json.put("success", false);
				json.put("msg", materService.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/cashierListForShop.do", method = { RequestMethod.POST })
	@ResponseBody
	public String cashierListForShop(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			CashierOrderVo cashierVo) throws GenericException {
		JSONObject json = new JSONObject();
		log.info("----");
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = cashierService.cashierListForShop(pageController, cashierVo);
		List<CashierOrderVo> cashierOrderVoList = new ArrayList<CashierOrderVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				CashierOrderVo f = new CashierOrderVo();
				f.setCashierId(Long.valueOf(item[0].toString()));
				f.setShopId(Long.valueOf(item[1].toString()));
				f.setShopName((String) item[2]);
				f.setUserId(Long.valueOf(item[3].toString()));
				f.setRealName((String) item[4]);
				f.setOrderValue((String) item[5]);
				f.setPayWay((Integer) item[6]);
				f.setSubmitTime((String) item[7]);
				cashierOrderVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", cashierOrderVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/preCashierOrderDetail.do", method = { RequestMethod.GET })
	public ModelAndView preCashierOrderDetail(CashierOrderVo cashierOrderVo, HttpSession session) {
		Map map = new HashMap();

		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			cashierOrderVo.setShopId(shopId);
			List<CashierOrderPo> list = cashierService.getCashierOrderPo(cashierOrderVo);
			map.put("cashierOrderPo", list.get(0));
		} catch (GenericException e) {
			e.printStackTrace();
		}

		return new ModelAndView("/cashier/cashierOrderDetailIndex", map);
	}

	@RequestMapping(value = "/cashierListDetailForShop.do", method = { RequestMethod.POST })
	@ResponseBody
	public String cashierListDetailForShop(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			CashierOrderDetailVo cashierOrderDetailVo) throws GenericException {
		JSONObject json = new JSONObject();
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = cashierService.cashierListDetailForShop(pageController, cashierOrderDetailVo);

		List<CashierOrderDetailVo> cashierOrderDetailVoList = new ArrayList<CashierOrderDetailVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				CashierOrderDetailVo f = new CashierOrderDetailVo();
				f.setCashierDetailId(Long.valueOf(item[0].toString()));
				f.setCashierId(Long.valueOf(item[1].toString()));
				f.setFoodId((Integer) item[2]);
				f.setFoodName((String) item[3]);
				f.setFoodNumber((String) item[4]);
				f.setFoodValue((String) item[5]);

				cashierOrderDetailVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", cashierOrderDetailVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/findFoodCashier.do", method = { RequestMethod.POST })
	@ResponseBody
	public String findFoodCashier(@RequestParam(required = false) String page, @RequestParam(required = false) String rows, FoodVo foodVo,
			HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
		foodVo.setShopId(shopId);
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = cashierService.findFoodCashier(pageController, foodVo);

		List<FoodVo> cashierOrderDetailVoList = new ArrayList<FoodVo>();
		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				FoodVo f = new FoodVo();
				f.setFoodId(Integer.valueOf(item[0].toString()));
				f.setFoodName(String.valueOf(item[1].toString()));
				f.setFoodValue((BigDecimal) item[2]);
				f.setFoodType((String) item[3]);
				f.setFoodTypeName((String) item[4]);
				f.setIs_active((Integer) item[5]);
				cashierOrderDetailVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", cashierOrderDetailVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/deleteFoodCashier.do", method = { RequestMethod.POST })
	@ResponseBody
	public String deleteFoodCashier(FoodVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			c.setShopId(shopId);
			// foodService.deleteShoppingCartCashierPo(c);
			cashierService.deleteFoodCashier(c);

			json.put("success", true);

		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/preUpdateCashierFood.do", method = { RequestMethod.GET })
	public ModelAndView preUpdateCashierFood(FoodVo vo, HttpSession session) {
		Map map = new HashMap();

		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			vo.setShopId(shopId);
			List<FoodPo> list = foodService.getFoodPo(vo);
			map.put("FoodPo", list.get(0));
		} catch (GenericException e) {
			e.printStackTrace();
		}

		return new ModelAndView("/cashier/updateCashierFood", map);
	}

	@RequestMapping(value = "/updateCashierFood.do", method = { RequestMethod.POST })
	@ResponseBody
	public String updateCashierFood(FoodVo c, HttpSession session) {
		JSONObject json = new JSONObject();
		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			c.setShopId(shopId);
			// foodService.deleteShoppingCartCashierPo(c);
			cashierService.updateFoodCashier(c);

			json.put("success", true);

		} catch (Exception e) {
			e.printStackTrace();
			json.put("success", false);
		}
		return json.toString();
	}

	@RequestMapping(value = "/goCashierStatisticsDetail.do", method = { RequestMethod.GET })
	public ModelAndView goCashierStatisticsDetail(CashierOrderDetailVo vo, HttpSession session) {
		Map map = new HashMap();

		try {
			UserPo userInfo = (UserPo) session.getAttribute("user");
			long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
			vo.setShopId(shopId);
			// List<FoodPo> list = foodService.getFoodPo(vo);
			map.put("CashierOrderDetailVo", vo);
		} catch (GenericException e) {
			e.printStackTrace();
		}

		return new ModelAndView("/cashier/cashierStatisticsDetail", map);
	}

	@RequestMapping(value = "/cashierListStatisticsForShop.do", method = { RequestMethod.POST })
	@ResponseBody
	public String cashierListStatisticsForShop(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			CashierOrderDetailVo cashierOrderDetailVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
		cashierOrderDetailVo.setShopId(shopId);
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		List<CashierOrderDetailVo> cashierOrderDetailVoList = new ArrayList<CashierOrderDetailVo>();
		if (cashierOrderDetailVo.getInitFlag() == 0) {
			json.put("success", false);
			json.put("total", 0);
			json.put("rows", cashierOrderDetailVoList);
			return json.toString();
		}

		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = cashierService.cashierListStatisticsForShop(pageController, cashierOrderDetailVo);

		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				CashierOrderDetailVo f = new CashierOrderDetailVo();
				f.setCashierDetailId(Long.valueOf(item[0].toString()));
				f.setCashierId(Long.valueOf(item[1].toString()));
				f.setFoodId((Integer) item[2]);
				f.setFoodName((String) item[3]);
				f.setFoodNumber(((Double) item[4]).toString());
				f.setFoodValueAll(((Double) item[5]).toString());
				f.setFoodTypeName((String) item[6]);
				f.setDateEnd(cashierOrderDetailVo.getDateEnd());
				f.setDateStart(cashierOrderDetailVo.getDateStart());
				f.setPayWay(cashierOrderDetailVo.getPayWay());

				cashierOrderDetailVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", cashierOrderDetailVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

	@RequestMapping(value = "/cashierListStatisticsDetailForShop.do", method = { RequestMethod.POST })
	@ResponseBody
	public String cashierListStatisticsDetailForShop(@RequestParam(required = false) String page, @RequestParam(required = false) String rows,
			CashierOrderDetailVo cashierOrderDetailVo, HttpSession session) throws GenericException {
		JSONObject json = new JSONObject();
		UserPo userInfo = (UserPo) session.getAttribute("user");
		long shopId = shopService.getShopIdoByUserId(userInfo.getUserId());
		cashierOrderDetailVo.setShopId(shopId);
		if (page == null || page.equals(""))
			page = "1";
		if (rows == null || rows.equals(""))
			rows = "10";
		List<CashierOrderDetailVo> cashierOrderDetailVoList = new ArrayList<CashierOrderDetailVo>();

		PageController pageController = new PageController(Integer.parseInt(page), Integer.parseInt(rows));
		this.pageList = cashierService.cashierListStatisticsDetailForShop(pageController, cashierOrderDetailVo);

		List items = pageList.getItems();
		if (items != null) {
			for (int i = 0; i < items.size(); i++) {
				Object[] item = (Object[]) items.get(i);
				CashierOrderDetailVo f = new CashierOrderDetailVo();
				f.setCashierDetailId(Long.valueOf(item[0].toString()));
				f.setCashierId(Long.valueOf(item[1].toString()));
				f.setFoodId((Integer) item[2]);
				f.setFoodName((String) item[3]);
				f.setFoodNumber((String) item[4]);
				f.setFoodValue((String) item[5]);
				f.setFoodValueAll(((Double) item[6]).toString());
				f.setSubmitTime((String) item[7]);
				cashierOrderDetailVoList.add(f);
			}
		}
		json.put("success", true);
		json.put("total", pageList.getTotalCount());
		json.put("rows", cashierOrderDetailVoList);
		log.info("----" + json.toString());
		return json.toString();
	}

}
